﻿namespace PlugNT.Cms.Core
{
    using PlugNT.Common;
    using PlugNT.Common.Templet;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Web;

    public class HtmlBuilder
    {
        public string ChannelNo = "";
        public string CreatHtmlDir = "";
        public string HtmlPageName = "";
        public string HtmlTempletPath = "";
        public int IdValue = -1;
        public string TablePostFix = "";

        private void CreateFile(string pageContent, string extName)
        {
            Encoding encoding = Encoding.UTF8;
            StreamWriter writer = null;
            string str = this.HtmlPageName + extName;
            try
            {
                string path = GetPath(this.CreatHtmlDir);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string str3 = path.EndsWith("/") ? (path + str) : (path + "/" + str);
                writer = new StreamWriter(str3, false, encoding);
                writer.Write(pageContent);
                writer.Flush();
                writer.Close();
            }
            catch (Exception exception)
            {
                writer = null;
                throw new HtmlException(exception.Message);
            }
            finally
            {
                writer = null;
            }
        }

        public void CreateListHtml()
        {
            if (this.HtmlTempletPath.Trim() == "")
            {
                throw new HtmlException("HtmlTempletPath路径为空生成列表页面失败");
            }
            string path = GetPath(this.HtmlTempletPath);
            if (!File.Exists(path))
            {
                throw new HtmlException("设置HtmlTempletPath路径错误生成列表页面失败");
            }
            string extName = this.GetExtName(this.HtmlTempletPath);
            bool zipFile = (extName != ".js") && (extName != ".css") && (extName != ".shtml");
            string html = GetHtml(path, zipFile);
            if (HasFieldTag(html))
            {
                throw new HtmlException("列表页中不能有聚合页或内容页的标签");
            }
            if (!HasPageListTag(html))
            {
                throw new HtmlException("当前列表页中没有有列表页的标签");
            }
            html = this.ReplacePublicTemplet(html);
            html = this.ReplaceCurrentTemplet(html);
            html = this.ReplaceShowTemplet(html);
            if (this.HtmlPageName == "")
            {
                throw new HtmlException("生成列表页面名称不能为空");
            }
            string htmlPageName = this.HtmlPageName;
            List<string> replaceListTemplets = this.GetReplaceListTemplets(html);
            int count = replaceListTemplets.Count;
            for (int i = 0; i < count; i++)
            {
                this.HtmlPageName = htmlPageName + ((i + 1)).ToString();
                this.CreateFile(replaceListTemplets[i], extName);
            }
        }

        public void CreateShowHtml()
        {
            string extName = this.GetExtName(this.HtmlTempletPath);
            if (this.HtmlTempletPath.Trim() == "")
            {
                throw new HtmlException("HtmlTempletPath路径为空生成内容页面失败");
            }
            string path = GetPath(this.HtmlTempletPath);
            if (!File.Exists(path))
            {
                throw new HtmlException("设置HtmlTempletPath路径错误生成内容页面失败");
            }
            bool zipFile = false;// (extName != ".js") && (extName != ".css");
            string html = GetHtml(path, zipFile);
            if (HasPageListTag(html) || HasPaginationTag(html))
            {
                throw new HtmlException("生成单个页面的内容页不能有列表页的标签");
            }
            this.HtmlPageName = this.HtmlPageName.Trim();
            if (this.HtmlPageName == "")
            {
                throw new HtmlException("生成内容页面名称不能为空");
            }
            html = this.ReplacePublicTemplet(html);
            html = this.ReplaceCurrentTemplet(html);
            html = this.ReplaceShowTemplet(html);
            this.CreateFile(html, extName);
        }

        public void CreateSingleHtml()
        {
            if (this.HtmlTempletPath.Trim() == "")
            {
                throw new HtmlException("HtmlTempletPath路径为空生成聚合页面失败");
            }
            string path = GetPath(this.HtmlTempletPath);
            if (!File.Exists(path))
            {
                throw new HtmlException("设置HtmlTempletPath路径错误生成聚合页面失败");
            }
            string extName = this.GetExtName(this.HtmlTempletPath);
            bool zipFile = false;// (extName != ".js") && (extName != ".css") ;
            string html = GetHtml(path, zipFile);
            if (HasPageListTag(html) || HasPaginationTag(html))
            {
                throw new HtmlException("生成单个页面的聚合页不能有列表页的标签");
            }
            this.HtmlPageName = this.HtmlPageName.Trim();
            if (this.HtmlPageName == "")
            {
                throw new HtmlException("生成聚合页面名称不能为空");
            }
            html = this.ReplacePublicTemplet(html);
            html = this.ReplaceCurrentTemplet(html);
            html = this.ReplaceShowTemplet(html);
            this.CreateFile(html, extName);
        }

        private string GetConditionValue(string expression, string tmpValue)
        {
            string[] strArray = expression.Split(new char[] { ',' });
            string str = tmpValue;
            switch (strArray[0])
            {
                case "eq":
                    if (!(strArray[2] == "now"))
                    {
                        if (tmpValue.ToLower() == strArray[2].ToLower())
                        {
                            return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                        }
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    if (!(tmpValue.ToLower() == DateTime.Now.ToString()))
                    {
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    return ((strArray[3] == "this") ? tmpValue : strArray[3]);

                case "neq":
                    if (!(strArray[2] == "now"))
                    {
                        if (tmpValue.ToLower() != strArray[2].ToLower())
                        {
                            return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                        }
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    if (!(tmpValue.ToLower() != DateTime.Now.ToString()))
                    {
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    return ((strArray[3] == "this") ? tmpValue : strArray[3]);

                case "gt":
                    if (!(strArray[1] == "integer"))
                    {
                        if (!(strArray[1] == "datetime"))
                        {
                            return str;
                        }
                        if (strArray[2] == "now")
                        {
                            if (DateTime.Parse(tmpValue) > DateTime.Now)
                            {
                                return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                            }
                            return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                        }
                        if (DateTime.Parse(tmpValue) > DateTime.Parse(strArray[2]))
                        {
                            return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                        }
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    if (int.Parse(tmpValue) <= int.Parse(strArray[2]))
                    {
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    return ((strArray[3] == "this") ? tmpValue : strArray[3]);

                case "egt":
                    if (!(strArray[1] == "integer"))
                    {
                        if (!(strArray[1] == "datetime"))
                        {
                            return str;
                        }
                        if (strArray[2] == "now")
                        {
                            if (DateTime.Parse(tmpValue) >= DateTime.Now)
                            {
                                return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                            }
                            return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                        }
                        if (DateTime.Parse(tmpValue) >= DateTime.Parse(strArray[2]))
                        {
                            return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                        }
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    if (int.Parse(tmpValue) < int.Parse(strArray[2]))
                    {
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    return ((strArray[3] == "this") ? tmpValue : strArray[3]);

                case "lt":
                    if (!(strArray[1] == "integer"))
                    {
                        if (!(strArray[1] == "datetime"))
                        {
                            return str;
                        }
                        if (strArray[2] == "now")
                        {
                            if (DateTime.Parse(tmpValue) < DateTime.Now)
                            {
                                return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                            }
                            return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                        }
                        if (DateTime.Parse(tmpValue) < DateTime.Parse(strArray[2]))
                        {
                            return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                        }
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    if (int.Parse(tmpValue) >= int.Parse(strArray[2]))
                    {
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    return ((strArray[3] == "this") ? tmpValue : strArray[3]);

                case "elt":
                    if (!(strArray[1] == "integer"))
                    {
                        if (!(strArray[1] == "datetime"))
                        {
                            return str;
                        }
                        if (strArray[2] == "now")
                        {
                            if (DateTime.Parse(tmpValue) <= DateTime.Now)
                            {
                                return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                            }
                            return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                        }
                        if (DateTime.Parse(tmpValue) <= DateTime.Parse(strArray[2]))
                        {
                            return ((strArray[3] == "this") ? tmpValue : strArray[3]);
                        }
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    if (int.Parse(tmpValue) > int.Parse(strArray[2]))
                    {
                        return ((strArray[4] == "this") ? tmpValue : strArray[4]);
                    }
                    return ((strArray[3] == "this") ? tmpValue : strArray[3]);

                case "cut":
                    {
                        int startIndex = int.Parse(strArray[1]);
                        int length = int.Parse(strArray[2]);
                        if (tmpValue.Length > length)
                        {
                            str = tmpValue.Substring(startIndex, length) + strArray[3];
                        }
                        return str;
                    }
                case "format":
                    return DateTime.Parse(tmpValue).ToString(strArray[1]);
            }
            return str;
        }

        private string GetExtName(string htmlTempPath)
        {
            int startIndex = htmlTempPath.LastIndexOf(".");
            string str = (startIndex > 0) ? htmlTempPath.Substring(startIndex) : htmlTempPath;
            str = str.ToLower();
            if (str == ".htm")
            {
                str = ".shtml";
            }
            return str;
        }

        private static string GetHtml(string path, bool zipFile)
        {
            string html = Utils.ReadFile(path);
            if (zipFile)
            {
                return Utils.ZipHtml(html);
            }
            return html;
        }

        private static string GetPath(string url)
        {
            return HttpContext.Current.Server.MapPath(url);
        }

        private List<string> GetReplaceListTemplets(string template)
        {
            List<string> list = new List<string>();
            if (template.Contains("{tmp:pagelist"))
            {
                List<RegTag> regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:pagelist");
                if (regTagsDistinct.Count != 1)
                {
                    throw new HtmlException("在模板中未找到tmp:pagelist的标签，或标签个数大于1。");
                }
                RegTag tag = regTagsDistinct[0];
                string attribute = RegTemplet.GetAttribute(tag.Attributes, "pagesize");
                string str2 = RegTemplet.GetAttribute(tag.Attributes, "exact");
                string expression = RegTemplet.GetAttribute(tag.Attributes, "count");
                string type = RegTemplet.GetAttribute(tag.Attributes, "type");
                string sortString = GetSortString(RegTemplet.GetAttribute(tag.Attributes, "sort"));
                if ((expression != "-1") && !(Utils.IsNumeric(attribute) && Utils.IsNumeric(expression)))
                {
                    throw new HtmlException("在pagelist标签中pagesize属性或count属性只能是大于等于0或-1的数字！");
                }
                int topCount = int.Parse(expression);
                bool isexact = str2.ToLower() == "true";
                DataTable table = DataAccess.GetContentTable(topCount, this.ChannelNo, type, sortString, this.TablePostFix, isexact);
                int count = table.Rows.Count;
                int pagesize = int.Parse(attribute);
                int currentpage = 1;
                List<RegTag> list3 = RegTemplet.GetRegTagsDistinct(tag.InnerText, "tmp:item");
                List<RegTag> list4 = RegTemplet.GetRegTagsDistinct(tag.InnerText, "tmp:rowcount");
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                foreach (RegTag tag2 in list3)
                {
                    string key = RegTemplet.GetAttribute(tag2.Attributes, "name");
                    if (key == "")
                    {
                        throw new HtmlException("列表中未设置字段名。");
                    }
                    string str7 = RegTemplet.GetAttribute(tag2.Attributes, "expression");
                    if (str7 == "")
                    {
                        if (dictionary.ContainsKey(key))
                        {
                            throw new HtmlException("列表中有同一字段不同的标记。");
                        }
                        dictionary.Add(key, tag2.OuterText);
                    }
                    else
                    {
                        if (dictionary.ContainsKey(key + "," + str7))
                        {
                            throw new HtmlException("列表中有同一字段不同的标记。");
                        }
                        dictionary.Add(key + "," + str7, tag2.OuterText);
                    }
                }
                List<RegTag> list5 = RegTemplet.GetRegTagsDistinct(template, "tmp:pagination");
                if (count == 0)
                {
                    template = RegTemplet.Replace(template, tag.OuterText, "");
                    foreach (RegTag tag3 in list5)
                    {
                        template = RegTemplet.Replace(template, tag3.OuterText, "");
                    }
                    list.Add(template);
                    return list;
                }
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < count; i++)
                {
                    string str10;
                    string str11;
                    DataRow row = table.Rows[i];
                    string innerText = tag.InnerText;
                    foreach (RegTag tag4 in list4)
                    {
                        innerText = RegTemplet.Replace(innerText, tag4.OuterText, (i + 1).ToString());
                    }
                    foreach (KeyValuePair<string, string> pair in dictionary)
                    {
                        int index = pair.Key.IndexOf(',');
                        string[] strArray = pair.Key.Split(new char[] { ',' }, 2);
                        if (!table.Columns.Contains(strArray[0]))
                        {
                            throw new HtmlException("数据库列表中不存在" + pair.Key + "的字段。");
                        }
                        if (strArray.Length != 2)
                        {
                            innerText = RegTemplet.Replace(innerText, pair.Value, row[strArray[0]].ToString());
                        }
                        else
                        {
                            string conditionValue = this.GetConditionValue(strArray[1], row[strArray[0]].ToString());
                            innerText = RegTemplet.Replace(innerText, pair.Value, conditionValue);
                        }
                    }
                    builder.Append(innerText);
                    if (((i + 1) % pagesize) == 0)
                    {
                        str10 = template;
                        str11 = "";
                        foreach (RegTag tag3 in list5)
                        {
                            string str12 = RegTemplet.GetAttribute(tag3.Attributes, "type");
                            if (!(str12 != ""))
                            {
                                goto Label_0567;
                            }
                            string str14 = str12;
                            if (str14 == null)
                            {
                                goto Label_054E;
                            }
                            if (!(str14 == "simple"))
                            {
                                if (str14 == "complex")
                                {
                                    goto Label_0538;
                                }
                                goto Label_054E;
                            }
                            str11 = Pagination(count, pagesize, currentpage, this.HtmlPageName, false);
                            goto Label_057D;
                        Label_0538:
                            str11 = Pagination(count, pagesize, currentpage, this.HtmlPageName, true);
                            goto Label_057D;
                        Label_054E:
                            str11 = Pagination(count, pagesize, currentpage, this.HtmlPageName, true);
                            goto Label_057D;
                        Label_0567:
                            str11 = Pagination(count, pagesize, currentpage, this.HtmlPageName, true);
                        Label_057D:
                            str10 = RegTemplet.Replace(str10, tag3.OuterText, str11);
                        }
                        list.Add(RegTemplet.Replace(str10, tag.OuterText, builder.ToString()));
                        builder.Remove(0, builder.Length);
                        currentpage++;
                    }
                    else if (i == (count - 1))
                    {
                        str10 = template;
                        str11 = Pagination(count, pagesize, currentpage, this.HtmlPageName, true);
                        foreach (RegTag tag3 in list5)
                        {
                            str10 = RegTemplet.Replace(str10, tag3.OuterText, str11);
                        }
                        list.Add(RegTemplet.Replace(str10, tag.OuterText, builder.ToString()));
                    }
                }
            }
            return list;
        }

        private static string GetSortString(string sortString)
        {
            if (sortString != "")
            {
                string[] strArray = sortString.Split(new char[] { ',' });
                StringBuilder builder = new StringBuilder();
                foreach (string str in strArray)
                {
                    string str2 = str.Trim();
                    if (str2 != "")
                    {
                        string[] strArray2 = str2.Split(new char[] { ' ' });
                        int length = strArray2.Length;
                        if (length != 1)
                        {
                            if (strArray2[length - 1] != "asc")
                            {
                                if (strArray2[length - 1] != "desc")
                                {
                                    throw new HtmlException("排序关键字错误SQL排序不能使用" + strArray2[length - 1] + "的关键字。");
                                }
                                builder.Append(strArray2[0] + " desc,");
                            }
                            else
                            {
                                builder.Append(strArray2[0] + " asc,");
                            }
                        }
                        else
                        {
                            builder.Append(strArray2[0] + " asc,");
                        }
                    }
                }
                return builder.ToString().Trim(new char[] { ',' });
            }
            return sortString;
        }

        private static bool HasFieldTag(string templet)
        {
            bool flag = false;
            if (templet.Contains("{tmp:field"))
            {
                flag = true;
            }
            return flag;
        }

        private static bool HasPageListTag(string templet)
        {
            bool flag = false;
            if (templet.Contains("{tmp:pagelist"))
            {
                flag = true;
            }
            return flag;
        }

        private static bool HasPaginationTag(string templet)
        {
            bool flag = false;
            if (templet.Contains("{tmp:pagination"))
            {
                flag = true;
            }
            return flag;
        }

        private static string Pagination(int recordcount, int pagesize, int currentpage, string prefix, bool showcount)
        {
            int num = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            StringBuilder builder = new StringBuilder();
            if (currentpage < 1)
            {
                currentpage = 1;
            }
            if (pagesize != 0)
            {
                num = recordcount / pagesize;
                num = ((recordcount % pagesize) != 0) ? (num + 1) : num;
                num = (num == 0) ? 1 : num;
            }
            num2 = currentpage + 1;
            num3 = currentpage - 1;
            num4 = ((currentpage + 5) > num) ? (num - 9) : (currentpage - 4);
            num5 = (currentpage < 5) ? 10 : (currentpage + 5);
            if (num4 < 1)
            {
                num4 = 1;
            }
            if (num < num5)
            {
                num5 = num;
            }
            if (showcount)
            {
                builder.Append("<span class='pcount'>共" + num + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>");
            }
            if (currentpage > 1)
            {
                builder.Append(string.Concat(new object[] { "<a href=\"", prefix, "1.shtml\">首页</a>&nbsp;&nbsp;<a href=\"", prefix, num3, ".shtml\">上一页</a>" }));
            }
            else
            {
                builder.Append("首页　上一页");
            }
            for (int i = num4; i <= num5; i++)
            {
                if (currentpage == i)
                {
                    builder.Append("&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>");
                }
                else
                {
                    builder.Append(string.Concat(new object[] { "&nbsp;&nbsp;<a href=\"", prefix, i, ".shtml\">", i, "</a>" }));
                }
            }
            if (currentpage != num)
            {
                builder.Append(string.Concat(new object[] { "&nbsp;&nbsp;<a href=\"", prefix, num2.ToString(), ".shtml\">下一页</a>&nbsp;&nbsp;<a href=\"", prefix, num, ".shtml\">末页</a>" }));
            }
            else
            {
                builder.Append("　下一页　末页");
            }
            return builder.ToString();
        }

        private string ReplaceCurrentTemplet(string template)
        {
            List<RegTag> regTagsDistinct = null;
            if (template.Contains("{tmp:looplist"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:looplist");
                foreach (RegTag tag in regTagsDistinct)
                {
                    DataTable contentTable = null;
                    string attribute = RegTemplet.GetAttribute(tag.Attributes, "sqlkey");
                    if (attribute != "")
                    {
                        contentTable = DataAccess.GetContentTable(attribute);
                    }
                    else
                    {
                        string expression = RegTemplet.GetAttribute(tag.Attributes, "count");
                        string clsno = RegTemplet.GetAttribute(tag.Attributes, "class");
                        string str4 = RegTemplet.GetAttribute(tag.Attributes, "exact");
                        string type = RegTemplet.GetAttribute(tag.Attributes, "type");
                        string sortString = GetSortString(RegTemplet.GetAttribute(tag.Attributes, "sort"));
                        if ((expression != "-1") && !Utils.IsNumeric(expression))
                        {
                            throw new HtmlException("在looplist标签中count属性只能是大于等于0或-1的数字！");
                        }
                        int topCount = int.Parse(expression);
                        bool isexact = str4.ToLower() == "true";
                        contentTable = (clsno.Trim() != "") ? DataAccess.GetContentTable(topCount, clsno, type, sortString, this.TablePostFix, isexact) : DataAccess.GetContentTable(topCount, this.ChannelNo, type, sortString, this.TablePostFix, isexact);
                    }
                    if (contentTable.Rows.Count > 0)
                    {
                        StringBuilder builder = new StringBuilder();
                        List<RegTag> list2 = RegTemplet.GetRegTagsDistinct(tag.InnerText, "tmp:item");
                        List<RegTag> list3 = RegTemplet.GetRegTagsDistinct(tag.InnerText, "tmp:rowcount");
                        Dictionary<string, string> dictionary = new Dictionary<string, string>();
                        foreach (RegTag tag2 in list2)
                        {
                            string key = RegTemplet.GetAttribute(tag2.Attributes, "name");
                            if (key == "")
                            {
                                throw new HtmlException("列表中未设置字段名。");
                            }

                            string str8 = RegTemplet.GetAttribute(tag2.Attributes, "expression");
                            if (str8 == "")
                            {
                                if (dictionary.ContainsKey(key))
                                {
                                    throw new HtmlException("列表中有同一字段不同的标记。");
                                }
                                dictionary.Add(key, tag2.OuterText);
                            }
                            else
                            {
                                if (dictionary.ContainsKey(key + "," + str8))
                                {
                                    throw new HtmlException("列表中有同一字段不同的标记。");
                                }
                                dictionary.Add(key + "," + str8, tag2.OuterText);
                            }
                        }
                        int num3 = 1;
                        foreach (DataRow row in contentTable.Rows)
                        {
                            string innerText = tag.InnerText;
                            foreach (RegTag tag3 in list3)
                            {
                                innerText = RegTemplet.Replace(innerText, tag3.OuterText, num3.ToString());
                            }
                            foreach (KeyValuePair<string, string> pair in dictionary)
                            {
                                int index = pair.Key.IndexOf(',');
                                string[] strArray = pair.Key.Split(new char[] { ',' }, 2);
                                if (!contentTable.Columns.Contains(strArray[0]))
                                {
                                    throw new HtmlException("数据库列表中不存在" + pair.Key + "的字段。");
                                }
                                if (strArray.Length != 2)
                                {
                                    innerText = RegTemplet.Replace(innerText, pair.Value, row[strArray[0]].ToString());
                                }
                                else
                                {
                                    string conditionValue = this.GetConditionValue(strArray[1], row[strArray[0]].ToString());
                                    innerText = RegTemplet.Replace(innerText, pair.Value, conditionValue);
                                }
                            }
                            builder.Append(innerText);
                            num3++;
                        }
                        template = RegTemplet.Replace(template, tag.OuterText, builder.ToString());
                    }
                    else
                    {
                        template = RegTemplet.Replace(template, tag.OuterText, "");
                    }
                }
            }
            if (template.Contains("{tmp:navigation"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:navigation");
                string navigation = DataAccess.GetNavigation(this.ChannelNo);
                if (navigation != "")
                {
                    foreach (RegTag tag in regTagsDistinct)
                    {
                        template = RegTemplet.Replace(template, tag.OuterText, navigation);
                    }
                }
                else
                {
                    foreach (RegTag tag in regTagsDistinct)
                    {
                        template = RegTemplet.Replace(template, tag.OuterText, "");
                    }
                }
            }
            if (template.Contains("{tmp:createdir"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:createdir");
                foreach (RegTag tag in regTagsDistinct)
                {
                    template = RegTemplet.Replace(template, tag.OuterText, this.CreatHtmlDir);
                }
            }
            return template;
        }

        private string ReplacePublicTemplet(string template)
        {
            List<RegTag> regTagsDistinct = null;
            if (template.Contains("{tmp:file"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:file");
                foreach (RegTag tag in regTagsDistinct)
                {
                    string attribute = RegTemplet.GetAttribute(tag.Attributes, "src");
                    if (attribute == "")
                    {
                        throw new HtmlException("未找到模板的模板，模板路径为空。");
                    }
                    attribute = GetPath(attribute);
                    if (!File.Exists(attribute))
                    {
                        throw new HtmlException("未找到" + attribute + "的模板。");
                    }
                    string extName = this.GetExtName(attribute);
                    bool zipFile = false;// (extName != ".js") && (extName != ".css");
                    template = RegTemplet.Replace(template, tag.OuterText, GetHtml(attribute, zipFile));
                }
            }
            if (template.Contains("{tmp:friendlink"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:friendlink");
                foreach (RegTag tag in regTagsDistinct)
                {
                    string clsno = RegTemplet.GetAttribute(tag.Attributes, "class");
                    if (clsno == "")
                    {
                        throw new HtmlException("友情链接内别未设置。");
                    }
                    template = RegTemplet.Replace(template, tag.OuterText, DataAccess.GetFriendLink(clsno, "  "));
                }
            }
            if (template.Contains("{tmp:productcatogry"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:productcatogry");
                foreach (RegTag tag in regTagsDistinct)
                {
                    string clsno = RegTemplet.GetAttribute(tag.Attributes, "class");
                    if (clsno == "")
                    {
                        throw new HtmlException("类别未设置。");
                    }
                    template = RegTemplet.Replace(template, tag.OuterText, DataAccess.GetProductCatory(clsno));
                }
            }
            //if (!template.Contains("{tmp:copyright"))
            //{
            //    throw new HtmlException("没有使用PlugNT的版权标记！");
            //}
            //regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:copyright");
            //if (regTagsDistinct.Count == 0)
            //{
            //    throw new HtmlException("没有使用PlugNT的版权标记！");
            //}
            foreach (RegTag tag in regTagsDistinct)
            {
                template = RegTemplet.Replace(template, tag.OuterText, DataAccess.GetCopyright());
            }
            return template;
        }

        private string ReplaceShowTemplet(string template)
        {
            List<RegTag> regTagsDistinct = null;
            if (template.Contains("{tmp:field"))
            {
                regTagsDistinct = RegTemplet.GetRegTagsDistinct(template, "tmp:field");
                DataTable contentTable = DataAccess.GetContentTable(this.IdValue, this.TablePostFix);
                if ((contentTable != null) && (contentTable.Rows.Count > 0))
                {
                    foreach (RegTag tag in regTagsDistinct)
                    {
                        string attribute = RegTemplet.GetAttribute(tag.Attributes, "name");
                        if (attribute == "")
                        {
                            throw new HtmlException("未设置字段名。");
                        }
                        if (!contentTable.Columns.Contains(attribute))
                        {
                            throw new HtmlException("数据库中不存在" + attribute + "的字段。");
                        }
                        string expression = RegTemplet.GetAttribute(tag.Attributes, "expression");
                        if (expression == "")
                        {
                            template = RegTemplet.Replace(template, tag.OuterText, contentTable.Rows[0][attribute].ToString());
                        }
                        else
                        {
                            string conditionValue = this.GetConditionValue(expression, contentTable.Rows[0][attribute].ToString());
                            template = RegTemplet.Replace(template, tag.OuterText, conditionValue);
                        }
                    }
                    return template;
                }
                foreach (RegTag tag in regTagsDistinct)
                {
                    template = RegTemplet.Replace(template, tag.OuterText, "");
                }
            }
            return template;
        }
    }
}

